package com.lmm.lcuser.lc_user.config;



import com.lmm.lcuser.lc_user.utils.RealmUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import io.realm.Realm;
import io.realm.RealmResults;

/**
 * Created by xmgong on 2017/8/31.
 */

public class AppInfoAction {

    public static Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        if (getToken().length() > 0) {
            params.put(AppKey.Token, getToken());
        }
        return params;
    }

    //注销用户对应操作
    public static void Logout() {
        AppKeyValueUtils.setValue(AppKey.LoginState, "0");
        AppKeyValueUtils.setValue(AppKey.Token, "");
        AppKeyValueUtils.setValue(AppKey.ShopKey, "");

    }

    public static String getToken() {
        return AppKeyValueUtils.getValue(AppKey.Token);
    }

    public static String getSpk() {
        return AppKeyValueUtils.getValue(AppKey.ShopKey);
    }

    /**
     * 添加历史城市
     *
     * @param s
     */
    public static void addHistoryKey(String s) {
        if (s.equals("")) {
            return;
        }
        String old = AppKeyValueUtils.getValue(AppKey.HistoryCitys);
        String newStr = "";
        if (old.equals("")) {
            newStr = s + ";";
        } else {
            if (old.contains(s)) {
                return;
            }
            if (old.split(";").length < 10) {
                newStr = old + s + ";";
            } else {
                //去掉第一个
                int index = old.indexOf(";");
                newStr = old.substring(index + 1) + s + ";";
            }
        }
        AppKeyValueUtils.setValue(AppKey.HistoryCitys, newStr);
    }

    public static ArrayList<String> getHistory() {
        ArrayList<String> items = new ArrayList<>();
        String[] s = AppKeyValueUtils.getValue(AppKey.HistoryCitys).split(";");
        for (String i : s) {
            if (i.length() > 0) {
                items.add(i);
            }
        }
        return items;
    }


    public static void clearnHistory() {
        AppKeyValueUtils.setValue(AppKey.HistoryCitys, "");
    }

    public static void delItemHistory(String item) {
        String old = AppKeyValueUtils.getValue(AppKey.HistoryCitys);
        if (old.contains(item)) {
            old = old.replace(item + ";", "");
        }
        AppKeyValueUtils.setValue(AppKey.HistoryCitys, old);
    }

    public static void setUserPhone(String phone) {
        AppKeyValueUtils.setValue(AppKey.Mobile, phone);
    }

    public static void setUserName(String userName) {
        AppKeyValueUtils.setValue(AppKey.UserName, userName);
    }

    public static void setUserSex(String sex) {
        AppKeyValueUtils.setValue(AppKey.UserSex, sex);
    }

    public static void setUserBirth(String birth) {
        AppKeyValueUtils.setValue(AppKey.UserBirth, birth);
    }

    public static boolean isLogin() {
        boolean isLogin = false;

        if (getToken().equals("") || AppKeyValueUtils.getValue(AppKey.LoginState).equals("") || AppKeyValueUtils.getValue(AppKey.LoginState).equals("0")) {
            isLogin = false;
        } else {
            isLogin = true;
        }
        return isLogin;
    }


//    public static String getDBCityName(String name) {
//        String dbname = "";
//        Realm realm = RealmUtils.getRealm();
//        realm.beginTransaction();
//        RealmResults<AreaRealm> results = realm.where(AreaRealm.class)
//                .equalTo("name", name)
//                .or()
//                .equalTo("name", name + "县")
//                .or()
//                .equalTo("name", name + "市")
//                .or()
//                .equalTo("name", name + "区")
//                .findAll();
////        RealmResults<AreaModel> results = realm.where(AreaModel.class).like("name", name + "*").findAll();
//        realm.commitTransaction();
//        if (results.size() > 0) {
//            dbname = results.get(0).getName();
//        } else {
//            dbname = "";
//        }
//        return dbname;
//    }

//    public static String getCityCodeByName(String cityname) {
//        Realm realm = RealmUtils.getRealm();
//        AreaRealm am = realm.where(AreaRealm.class).like("name", cityname + "*").findFirst();
//        return am.getCode();
//    }

    public static ArrayList<String> priceRanges() {
        ArrayList<String> prices = new ArrayList<>();
        if (prices.size() == 0) {
            prices.add(" 0 - 10 ");
            prices.add(" 10 - 20 ");
            prices.add(" 20 - 30 ");
        }
        return prices;
    }


//    public static List<RichTxtModel> getRichTxtLst(@NonNull String res) {
//        List<RichTxtModel> mRichTxtModelList = new ArrayList<>();
//        try {
//            JSONArray jsonArray = new JSONArray(res);
//
//            RichTxtModel rtm = null;
//            JSONObject item = null;
//            for (int i = 0; i < jsonArray.length(); i++) {
//                rtm = new RichTxtModel();
//                item = (JSONObject) jsonArray.get(i);
//                rtm.setRichType(item.getInt("type"));
//                rtm.setRichValue(item.getString("value"));
//                mRichTxtModelList.add(rtm);
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return mRichTxtModelList;
//    }


    //获取结算页面的参数
    public static String getSettleParams() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("couponIds", AppKeyValueUtils.getValue(AppKey.Settle_Params_ClientInfo_CouponIds).toString());
            jsonObject.put("useVM", AppKeyValueUtils.getValue(AppKey.Settle_Params_ClientInfo_UseVM).equals("1"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonObject.toString();
    }


    public static String getSettleParams(String couponId, String uservm) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("couponIds", couponId);
            jsonObject.put("useVM", uservm.equals("1"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonObject.toString();
    }

}

